package com.omg.api.entry;

import javax.servlet.http.HttpServletRequest;

import com.omg.api.argument.Argument;
import com.omg.api.argument.CommentArgument;
import com.omg.api.handle.CommentHandler;
import com.omg.constant.Constants;
import com.omg.constant.ResponseCode;

/**
 * 
 * @author roger
 * 
 */
public class comment extends Entry {

	private static final long serialVersionUID = 1L;

	@Override
	protected Argument fetchArgument(HttpServletRequest request)
			throws Exception {
		String userid = request.getParameter("userid");
		if (userid == null || userid.isEmpty()) {
			throw new Exception("" + ResponseCode.AP_E_Info_InvalidUserid);
		}

		String action = request.getParameter("action");
		if (action == null || action.isEmpty()) {
			throw new Exception("" + ResponseCode.AP_E_Info_InvalidAction);
		}
		if (!action.equals(Constants.ACTION_DELETE)
				&& !action.equals(Constants.ACTION_EDIT)
				&& !action.equals(Constants.ACTION_POST)
				&& !action.equals(Constants.ACTION_GET)) {
			throw new Exception("" + ResponseCode.AP_E_Info_InvalidAction);
		}

		String content = request.getParameter("content");
		if (!action.equals(Constants.ACTION_DELETE)
				&& !action.equals(Constants.ACTION_GET)
				&& (content == null || content.isEmpty())) {
			throw new Exception("" + ResponseCode.AP_E_Info_ContentIsEmpty);
		}

		String activityid = request.getParameter("activityid");
		if (action.equals(Constants.ACTION_POST)
				|| action.equals(Constants.ACTION_GET)) {
			if (activityid == null || activityid.isEmpty()) {
				throw new Exception(""
						+ ResponseCode.AP_E_Info_InvalidActivityId);
			}
		}

		String commentid = request.getParameter("commentid");
		if (action.equals(Constants.ACTION_DELETE) || action.equals("edit")) {
			if (commentid == null || commentid.isEmpty()) {
				throw new Exception(""
						+ ResponseCode.AP_E_Info_InvalidCommentId);
			}
		}

		CommentArgument argument = new CommentArgument();
		argument.setUserid(userid);
		argument.setContent(content);
		argument.setAction(action);
		argument.setActivityid(activityid);
		argument.setCommentid(commentid);

		return argument;
	}

	@Override
	protected String getResult(Argument argument) throws Exception {
		return CommentHandler.getInstance().handle(argument);
	}

}
